/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import BLO.ExamBLO;
import BLO.UserBLO;
import DAO.UserDAO;
import DTO.ExamDTO;
import DTO.UserDTO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Arturia
 */
public class Controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("Action");
            if (action.equals("Login")) {
                String username = request.getParameter("txtUsername");
                String password = request.getParameter("txtPassword");

                String url = "";
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(60 * 60); //session co gia tri trong 30 phut
                UserDTO user = new UserDTO();

                user = checkLogin(username, password);
                if (user != null) {
                    session.setAttribute("USERNAME", username);
                    session.setAttribute("LOGINFAIL", "false");
                    session.setAttribute("ROLEID", user.getStrRoleID());
                    if (user.getStrRole().equals("Student")) {
                        //redirect ve trang student
                        session.setAttribute("ROLE", "Student");
                        url = "viewCourses.jsp";
                    } else if (user.getStrRole().equals("Teacher")) {
                        //redirect ve trang teacher
                        session.setAttribute("ROLE", "Teacher");
                        url = "viewCourses.jsp";
                    }
                } else {
                    //login fail redirect ve trang login
                    session.setAttribute("LOGINFAIL", "true");
                    url = "index.jsp";
                }
                RequestDispatcher rd = request.getRequestDispatcher(url);
                rd.forward(request, response);
            } else if (action.equals("Logout")) {
                HttpSession session = request.getSession();
                session.invalidate();
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                rd.forward(request, response);
            } else if (action.equals("ViewDetail")) {
                HttpSession session = request.getSession();
                String subjectid = request.getParameter("subjectid");
                String subjectname = request.getParameter("subjectname");
                String description = request.getParameter("description");
                String role = (String) session.getAttribute("ROLE");

                ExamBLO eblo = new ExamBLO();
                if (role.equals("Student")) {
                    ArrayList<ExamDTO> list = eblo.getExamOfASubject(subjectid);
                    if (list.size() > 0) {
                        session.setAttribute("subjectname", subjectname);
                        session.setAttribute("description", description);
                        session.setAttribute("EXAMLIST", list);
                        RequestDispatcher rd = request.getRequestDispatcher("subjectdetail.jsp");
                        rd.forward(request, response);
                    } else {
                        RequestDispatcher rd = request.getRequestDispatcher("invalid.jsp");
                        rd.forward(request, response);
                    }
                }else if(role.equals("Teacher")){


                }
            }else if(action.equals("viewCourses")){
                HttpSession session = request.getSession();
                String username = (String) session.getAttribute("USERNAME");
                if(username !=  null){
                    RequestDispatcher rd = request.getRequestDispatcher("viewCourses.jsp");
                    rd.forward(request, response);
                }else{
                    session.setAttribute("LOGINFAIL", "true");
                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                    rd.forward(request, response);
                }
            }else if(action.equals("Quiz")){
                HttpSession session = request.getSession();
                String type = request.getParameter("Type");

                session.setAttribute("TYPE", type);
                RequestDispatcher rd = request.getRequestDispatcher("quiz.jsp");
                rd.forward(request, response);
            }
        } finally {
            out.close();
        }
    }

    private UserDTO checkLogin(String username, String password) {
        UserBLO ublo = new UserBLO();
        return ublo.checkLogin(username, password);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
